package tech.aistar.controller.collecte;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.News;
import tech.aistar.service.center.IUserInfoService;
import tech.aistar.service.newService.INewsService;
import tech.aistar.service.newService.INewsVoService;
import tech.aistar.vo.NewsVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2019/10/14 0014.
 */
@Controller
@RequestMapping("/news")
@SuppressWarnings("all")
public class NewController {
    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsVoService newsVoService;
    @Autowired
    private IUserInfoService userInfoService;
    @RequestMapping("/news")
    //查询所有内容
    public String xiao(Model model,HttpSession session){

        Integer uid = (Integer) session.getAttribute("userinfoId");
        List<NewsVo> vos = newsVoService.newvoFindOne(Integer.valueOf(uid));
//
//        List<UserInfo> userInfos = new ArrayList<>();
//        for (int i = 0; i <vos.size() ; i++) {
//            UserInfo sid = userInfoService.findById(vos.get(i).getSerid());
//            userInfos.add(sid);
//        }
//
//        model.addAttribute("userInfos",userInfos);
        model.addAttribute("vos",vos);

        return "collection/news";
    }
    @RequestMapping("/addnews")
    //添加信息
    public String addnew(Model model, HttpSession session, HttpServletRequest request){
        //获取user的id
        Integer uid = (Integer) session.getAttribute("userinfoId");
        //获取发送的内容
//        String content = request.getParameter("news");

        News news1 = new News();
        news1.setUserinfoId(uid);
//        news1.setNewsTime(new Date());
        news1.setStatus(0);
//        news1.getNewsContent(content);
        newsService.newsAdd(news1);
        return "redirect:/news/news";

    }
    @RequestMapping("/delnews")       //进行单个删除
    public String newsdel(Model model, HttpServletRequest request){

        //单个输出
        // 收藏
        String aid =  request.getParameter("id");
        List<News> newsList = newsService.newdelById(Integer.valueOf(aid));
        model.addAttribute("newsList",newsList);
        return "redirect:/news/news";
    }

    @RequestMapping("/delnewsany")      //进行多个删除
    public String coldelany(Model model,HttpServletRequest request) {
        String pathInfo2 = request.getPathInfo();
        String[] ids = request.getParameterValues("id");
        for(String s:ids){
            List<News> news2 = newsService.newdelById(Integer.valueOf(s));
            model.addAttribute("news2",news2);
            System.out.println("s:"+s);
        }

        return "redirect:/news/news";
    }

}
